package com.huangpeng.designmodel.highConcurrency;

import java.util.concurrent.CountDownLatch;

/**
 * @Author huangpeng
 * @Title CountDownLath
 * @Package
 * @Date 2021/1/25 10:34
 * @Description CountDwonLatch用来等待一个或者多个线程完成操作，常常用来控制线程等待，它可以让某个线程等待，直到其他线程执行任务结束，才开始执行
 * CountDwonLatch只要有两个方法，当一个或多个线程调用await()方法时，调用线程会被阻塞，其他线程调用countDown()方法会将计数器减1（调用countDown方法的线程不会阻塞），当计数器的值变为零时，因调用await()方法被阻塞的线程会被唤醒，继续执行。
 * @Version 1.0
 */
public class CountDownLatchDemo {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(6);
        for (int i = 1; i < 6; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName()+"国\t被灭 ");
                countDownLatch.countDown();
            }, CountryEnum.forEachEnum(i).getRetMessage()).start();
        }

        countDownLatch.await();
        System.out.println("秦国 \t一统天下  ");
    }
    public static void closeDoor() throws InterruptedException{
        CountDownLatch countDownLatch = new CountDownLatch(6);
        for (int i = 1; i < 6; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName()+"\t 号同学离开教室");
                countDownLatch.countDown();
            }, String.valueOf(i)).start();
        }

        countDownLatch.await();
        System.out.println(Thread.currentThread().getName()+"\t 同学走完班长关门");
    }
}
